{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Divide a String Into Groups of Size k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: divideString"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #将字符串拆分为若干长度为 k 的组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>字符串 <code>s</code> 可以按下述步骤划分为若干长度为 <code>k</code> 的组：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>第一组由字符串中的前 <code>k</code> 个字符组成，第二组由接下来的 <code>k</code> 个字符串组成，依此类推。每个字符都能够成为 <strong>某一个</strong> 组的一部分。</li>\n",
    "\t<li>对于最后一组，如果字符串剩下的字符 <strong>不足</strong> <code>k</code> 个，需使用字符 <code>fill</code> 来补全这一组字符。</li>\n",
    "</ul>\n",
    "\n",
    "<p>注意，在去除最后一个组的填充字符 <code>fill</code>（如果存在的话）并按顺序连接所有的组后，所得到的字符串应该是 <code>s</code> 。</p>\n",
    "\n",
    "<p>给你一个字符串 <code>s</code> ，以及每组的长度 <code>k</code> 和一个用于填充的字符 <code>fill</code> ，按上述步骤处理之后，返回一个字符串数组，该数组表示 <code>s</code> 分组后 <strong>每个组的组成情况</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"abcdefghi\", k = 3, fill = \"x\"\n",
    "<strong>输出：</strong>[\"abc\",\"def\",\"ghi\"]\n",
    "<strong>解释：</strong>\n",
    "前 3 个字符是 \"abc\" ，形成第一组。\n",
    "接下来 3 个字符是 \"def\" ，形成第二组。\n",
    "最后 3 个字符是 \"ghi\" ，形成第三组。\n",
    "由于所有组都可以由字符串中的字符完全填充，所以不需要使用填充字符。\n",
    "因此，形成 3 组，分别是 \"abc\"、\"def\" 和 \"ghi\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"abcdefghij\", k = 3, fill = \"x\"\n",
    "<strong>输出：</strong>[\"abc\",\"def\",\"ghi\",\"jxx\"]\n",
    "<strong>解释：</strong>\n",
    "与前一个例子类似，形成前三组 \"abc\"、\"def\" 和 \"ghi\" 。\n",
    "对于最后一组，字符串中仅剩下字符 'j' 可以用。为了补全这一组，使用填充字符 'x' 两次。\n",
    "因此，形成 4 组，分别是 \"abc\"、\"def\"、\"ghi\" 和 \"jxx\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n",
    "\t<li><code>s</code> 仅由小写英文字母组成</li>\n",
    "\t<li><code>1 &lt;= k &lt;= 100</code></li>\n",
    "\t<li><code>fill</code> 是一个小写英文字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [divide-a-string-into-groups-of-size-k](https://leetcode.cn/problems/divide-a-string-into-groups-of-size-k/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [divide-a-string-into-groups-of-size-k](https://leetcode.cn/problems/divide-a-string-into-groups-of-size-k/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abcdefghi\"\\n3\\n\"x\"', '\"abcdefghij\"\\n3\\n\"x\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res = []\n",
    "        index = 0\n",
    "        l = len(s)\n",
    "        while(index<l):\n",
    "            mx = min(l, index+k)\n",
    "            res.append(s[index:mx])\n",
    "            index += k\n",
    "        if len(res[-1])<k:\n",
    "            res[-1] = res[-1]+fill*(k-len(res[-1]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        return [s[i:i+k].ljust(k, fill) for i in range(0, len(s), k)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ret = []\n",
    "        for i in range(0,len(s),k):\n",
    "            cur = s[i:i+k].ljust(k,fill)\n",
    "            \n",
    "            ret.append(cur)\n",
    "        return ret\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res = []   # 分组后的字符串\n",
    "        n = len(s)\n",
    "        curr = 0   # 每个分组的起始下标\n",
    "        # 拆分字符串\n",
    "        while curr < n:\n",
    "            res.append(s[curr:curr+k])\n",
    "            curr += k\n",
    "        # 尝试填充最后一组\n",
    "        res[-1] += fill * (k - len(res[-1]))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        result = []\n",
    "        start = 0\n",
    "        while start < len(s) - k:\n",
    "            r = \"\"\n",
    "            for idx in range(k):\n",
    "                r += s[start+idx]\n",
    "            result.append(r)\n",
    "            start += k\n",
    "\n",
    "        result.append(s[start:].ljust(k, fill))\n",
    "        return result\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        count_a = 0\n",
    "        count_b = 0\n",
    "        res = list()\n",
    "        temp = str()\n",
    "        for i in s:\n",
    "            if count_b == k:\n",
    "                res.append(temp)\n",
    "                temp = \"\"\n",
    "                count_b = 0\n",
    "                count_a += 1\n",
    "            temp += i\n",
    "            count_b += 1\n",
    "        if 0 < count_b <= k:\n",
    "            temp += fill * (k - count_b)\n",
    "            res.append(temp)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        count_a = 0\n",
    "        count_b = 0\n",
    "        res = list()\n",
    "        temp = str()\n",
    "        for i in s:\n",
    "            if count_b == k:\n",
    "                res.append(temp)\n",
    "                temp = \"\"\n",
    "                count_b = 0\n",
    "                count_a += 1\n",
    "            temp += i\n",
    "            count_b += 1\n",
    "        if 0 < count_b <= k:\n",
    "            temp += fill * (k - count_b)\n",
    "            res.append(temp)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n = len(s)\n",
    "        ans = [s[i:i+k] for i in range(0, n, k)]\n",
    "        if n % k:\n",
    "            ans[-1] += fill * (k - len(ans[-1]))\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        rst = []\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            if(i%k == k-1):\n",
    "                rst.append(s[i-k+1:i+1])\n",
    "        if(len(s) % k != 0):\n",
    "            rst.append(s[n-(n%k):n+1])\n",
    "            for i in range(k - (n%k)):\n",
    "                rst[-1] = rst[-1] + fill\n",
    "\n",
    "        return rst        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        return [s[i:i+k].ljust(k, fill) for i in range(0, len(s), k)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n = len(s)\n",
    "        \n",
    "        res = []\n",
    "        for i in range(0, n, k):\n",
    "            if i + k <= n:\n",
    "                res.append(s[i : i+k])\n",
    "            else:\n",
    "                res.append(s[i : n] + fill * (k - (n - i)))\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        result =[]\n",
    "        length = len(s)//k     #至少是3的整数倍\n",
    "        remainder = len(s) - length*k #还需要多少个fill\n",
    "        for i in range(length):\n",
    "            element = \"\"\n",
    "            for j in range(k):\n",
    "                letter = s[i*k+j]\n",
    "                element = element + letter\n",
    "            result.append(element)\n",
    "        if remainder == 0:\n",
    "            return result\n",
    "        else:\n",
    "            last = s[-remainder:] +(k-remainder)*fill\n",
    "            result.append(last)\n",
    "            return result\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res=[]\n",
    "        i=0\n",
    "        while i < len(s):\n",
    "            res.append(s[i:i+k])\n",
    "            i+=k\n",
    "        if len(res[-1])<k:\n",
    "            res[-1]+=fill*(k-len(res[-1]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> list[str]:\n",
    "        returnlist=[]\n",
    "        while len(s)>=k:\n",
    "            returnlist.append(s[0:k])\n",
    "            s=s[k:]\n",
    "        if len(s)==0:\n",
    "            return returnlist\n",
    "        else:\n",
    "            returnlist.append(str(s) + (k-len(s)) * fill)\n",
    "            return returnlist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ret = []\n",
    "        for i in range(0, len(s), k):\n",
    "            if i + k > len(s):\n",
    "                ret.append(s[i:] + fill * (i + k - len(s)))\n",
    "            else:\n",
    "                ret.append(s[i:i + k])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n = len(s)\n",
    "        ans = []\n",
    "        for i in range(0, n, k):\n",
    "            tmp = ''\n",
    "            for j in range(i, min(i+k, n)):\n",
    "                tmp += s[j]\n",
    "            ans.append(tmp)\n",
    "        if n % k != 0:\n",
    "            r = k - n % k \n",
    "            ans[-1] += fill * r \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ans=[]\n",
    "        i=-1\n",
    "        for i in range(len(s)//k):\n",
    "            ans.append(s[i*k:(i+1)*k])\n",
    "        if len(s)-(i+1)*k>0:\n",
    "            ans.append(s[(i+1)*k:len(s)]+fill*((i+2)*k-len(s)))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        num = 0\n",
    "        length = len(s)\n",
    "        res = [''] * math.ceil(length / k)\n",
    "        for i in range(0, length, k):\n",
    "            if (i + k) <= length:\n",
    "                res[num] = s[i:i + k]\n",
    "                num += 1\n",
    "            else:\n",
    "                res[num] = s[i:length]\n",
    "                while len(res[num]) < k:\n",
    "                    res[num] += fill\n",
    "                break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        s = s + (k-len(s) % k)*fill if len(s) % k else s\n",
    "        return [s[i:i+k] for i in range(0,len(s),k)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        s = s + (k-len(s) % k)*fill if len(s) % k else s\n",
    "        return [s[i:i+k] for i in range(0,len(s),k)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        t=[]\n",
    "        while k<=len(s):\n",
    "            t.append(s[:k])\n",
    "            s=s[k:]\n",
    "        if len(s)==0:\n",
    "            return t\n",
    "        else:\n",
    "            t.append(s+(k-len(s))*fill)\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ans = [s[i:i+k] for i in range(0, len(s), k)]\n",
    "        if len(ans[-1]) < k:\n",
    "            ans[-1] = ans[-1].ljust(k, fill)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        li = []\n",
    "        s1 = []\n",
    "        word = \"\"\n",
    "        for j in s:\n",
    "            li.append(j)\n",
    "        cnt = len(li)\n",
    "        for i in range(len(li)):\n",
    "            cnt -= 1\n",
    "            word = word + li[i]\n",
    "            if len(word) == k:\n",
    "                s1.append(word)\n",
    "                word = \"\"\n",
    "            elif cnt < k:\n",
    "                if 0 < cnt < k and len(word) < k:\n",
    "                    pass\n",
    "                elif cnt <=0 and len(word) < k:\n",
    "                    for i in range(k):\n",
    "                        if len(word)<k:\n",
    "                            word = word + fill\n",
    "                    s1.append(word)\n",
    "\n",
    "        return s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res = []   # 分组后的字符串\n",
    "        n = len(s)\n",
    "        curr = 0   # 每个分组的起始下标\n",
    "        # 拆分字符串\n",
    "        while curr < n:\n",
    "            res.append(s[curr:curr+k])\n",
    "            curr += k\n",
    "        # 尝试填充最后一组\n",
    "        res[-1] += fill * (k - len(res[-1]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n = len(s) % k\n",
    "        if n != 0:\n",
    "            s = s + fill * (k-n)\n",
    "        m = len(s) // k\n",
    "        lst = [s[i*k:i*k+k] for i in range(m)]\n",
    "        return lst\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ans = []\n",
    "        while len(s) > 0 :\n",
    "            if len(s) >= k :\n",
    "                ans.append(s[:k])\n",
    "                s = s[k:]\n",
    "            else :\n",
    "                tmp = s\n",
    "                while len(tmp) < k : \n",
    "                    tmp += fill\n",
    "                ans.append(tmp)\n",
    "                break \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n = len(s)\n",
    "        s = s + fill * (k - n % k) if n % k != 0 else s\n",
    "        return [s[i * k: (i + 1) * k] for i in range(len(s) // k)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ans = [s[i:i+k] for i in range(0,len(s),k)]\n",
    "        if len(ans[-1]) < k:\n",
    "            ans[-1] += (k - len(ans[-1])) * fill\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        list = []\n",
    "        for i in range(0,len(s),k):\n",
    "            list.append(s[i:i+k])\n",
    "        if len(list[-1]) < k:\n",
    "            for i in range(k - len(list[-1])):\n",
    "                list[-1] += fill\n",
    "        return list\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res = [] #保存分组后的数据\n",
    "        n = len(s)\n",
    "        curr = 0\n",
    "        while curr < n:\n",
    "            res.append(s[curr:curr+k]) \n",
    "            print(s[curr:curr+k],curr,curr+k)\n",
    "            curr += k  \n",
    "        #补充最后一组\n",
    "        res[-1] += fill * (k - len(res[-1]))\n",
    "        return res\n",
    "\n",
    "\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/divide-a-string-into-groups-of-size-k/solutions/1233149/jiang-zi-fu-chuan-chai-fen-wei-ruo-gan-c-264k/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res = []   # 分组后的字符串\n",
    "        n = len(s)\n",
    "        curr = 0   # 每个分组的起始下标\n",
    "        # 拆分字符串\n",
    "        while curr < n:\n",
    "            res.append(s[curr:curr+k])\n",
    "            curr += k\n",
    "        # 尝试填充最后一组\n",
    "        res[-1] += fill * (k - len(res[-1]))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        rst = []\n",
    "        for i in range(len(s)):\n",
    "            if(i%k == k-1):\n",
    "                rst.append(s[i-k+1:i+1])\n",
    "        if(len(s) % k != 0):\n",
    "            rst.append(s[len(s)-(len(s)%k):len(s)+1])\n",
    "            for i in range(k - (len(s)%k)):\n",
    "                rst[-1] = rst[-1] + fill          \n",
    "        return rst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ret = []\n",
    "        for i in range(0,len(s),k):\n",
    "            cur = s[i:i+k]\n",
    "            if len(cur) < k:\n",
    "                cur = cur.ljust(k,fill)\n",
    "            ret.append(cur)\n",
    "        return ret\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n = len(s)\n",
    "        if n % k:\n",
    "            s += (k-n%k)*fill\n",
    "        ans = []\n",
    "        for i in range(0, len(s), k):\n",
    "            ans.append(s[i:i+k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res = []\n",
    "        n = len(s)\n",
    "        curr = 0\n",
    "        while curr < n:\n",
    "            res.append(s[curr:curr+k])\n",
    "            curr+=k\n",
    "        res[-1] += fill * (k - len(res[-1]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ans = []\n",
    "        s += fill * (k - len(s) % k if len(s) % k != 0 else 0)\n",
    "        for i in range(0, len(s), k):\n",
    "            ans.append(s[i: i + k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        l = len(s)\n",
    "        n = l//k\n",
    "        alist = []\n",
    "        for i in range(0,n*k,k):\n",
    "            alist.append(s[i:i+k])\n",
    "        SX = s[n*k:l]\n",
    "        \n",
    "        s = k-len(SX)\n",
    "        if s!=k:\n",
    "            SX = SX+fill*s\n",
    "            alist.append(SX)\n",
    "        return alist\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n = len(s)\n",
    "        r = []\n",
    "        for i in range(0,n,k):\n",
    "            if i + k <= n:\n",
    "                r.append(s[i : i + k])\n",
    "            else:\n",
    "                r.append(s[i : n] + fill * (k - (n - i)))\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        return [s[i:i+k].ljust(k,fill)  for i in range(0,len(s),k)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        fh = []\n",
    "        for i in range(0, len(s), k):\n",
    "            fh.append(s[i:i+k])\n",
    "        fh[-1] += fill*(k-len(fh[-1]))\n",
    "        return fh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n = len(s)\n",
    "        res = []\n",
    "        for i in range(0, n, k):\n",
    "            if i + k <= n:\n",
    "                res.append(s[i : i+k])\n",
    "            else:\n",
    "                res.append(s[i : n] + fill * (k - (n - i)))\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        return [(s + fill * (k - len(s) % k if len(s) % k != 0 else 0))[i: i + k] for i in range(0, len(s), k)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution: \n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ret = [s[i:i+k] for i in range(0,len(s),k)]\n",
    "        while len(ret[-1]) != k:\n",
    "            ret[-1] = ret[-1] + fill\n",
    "        return ret\n",
    "        # return [(s[i:i+k] if len(s) - i > k else (s[i:] + fill * (len(s) // k - (len(s) % k)))) for i in range(0,(len(s) // k + 1) * k,k)] if len(s) % k else [s[i:i+k] for i in range(0,len(s),k)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ans = []\n",
    "        cur = ''\n",
    "        for i in range(0, len(s)):\n",
    "            cur += s[i]\n",
    "            if (i+1) % k == 0:\n",
    "                ans.append(cur)\n",
    "                cur = '' \n",
    "        if cur:\n",
    "            while len(cur) < k:\n",
    "                cur += fill\n",
    "            ans.append(cur)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n=len(s)\n",
    "        x=n%k\n",
    "        s=s+fill*(k-x)  if x else s\n",
    "        ans=[]\n",
    "        for i in range(0,len(s),k):\n",
    "            ans.append(s[i:i+k])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        x = []\n",
    "        for i in range(len(s)//k):\n",
    "            x.append(s[i*k:(i+1)*k])\n",
    "        if (k-(len(s)%k))%k:\n",
    "            x.append(s[len(s)//k*k:] + (k-(len(s)%k))%k*fill)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        result = []\n",
    "        length = len(s)\n",
    "        p = 0\n",
    "        while p < length:\n",
    "            if p + k <= length:\n",
    "                result.append(s[p:p + k])\n",
    "\n",
    "            else:\n",
    "                rest = s[p:]\n",
    "                rest += fill * (k - len(rest))\n",
    "                result.append(rest)\n",
    "            p += k\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        return [s[i * k: (i + 1) * k] for i in range((len(s) - 1) // k)] + [s[((len(s) - 1) // k) * k:] + fill * (((len(s) - 1) // k + 1) * k - len(s))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ans=[]\n",
    "        for i in range(0,len(s),k):\n",
    "            ans.append(s[i:i+k])\n",
    "            print(s[i:i+k])\n",
    "        if len(ans[-1])!=k:\n",
    "            while len(ans[-1])!=k:\n",
    "                ans[-1]+=fill\n",
    "            return ans\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        num_fill = len(s) % k\n",
    "        if len(s) % k:\n",
    "            num_fill = k - num_fill\n",
    "            \n",
    "        s = s + fill*num_fill\n",
    "\n",
    "        ans = []\n",
    "        for i in range(0, len(s),k):\n",
    "            ans.append(s[i:i+k])\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        s = s + \"\".join(list(fill for _ in range(k-1)))\n",
    "        n = len(s)\n",
    "        ans = []\n",
    "        i = 0\n",
    "        while i + k <= n:\n",
    "            ans.append(s[i:i+k])\n",
    "            i += k\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res=[]\n",
    "        for i in range(0,len(s),k):\n",
    "            if i+k<=len(s):\n",
    "                res.append(s[i:i+k])\n",
    "            else:\n",
    "                t=s[i:]+fill*(k-(len(s)-i))\n",
    "                #\"\".join([fill]*(k-(len(s)-i)))\n",
    "                res.append(t)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        remain_str_num = len(s) % k  # calculate the number of groups it should be splited\n",
    "        group_num = int(len(s) / k)\n",
    "        print(f'group num: {group_num}')\n",
    "        # split them into different gruop by index\n",
    "        str_list = []\n",
    "        for i in range(group_num):\n",
    "            print(i)\n",
    "            str_split = s[i*k:i*k+k:1]\n",
    "            print(str_split)\n",
    "            str_list.append(s[i*k:i*k+k:1])\n",
    "\n",
    "        # if string remain at last, fill it with 'fill'\n",
    "        if remain_str_num != 0:\n",
    "            str_split = s[-remain_str_num::] + fill * (k - remain_str_num)\n",
    "            str_list.append(str_split)\n",
    "\n",
    "        return str_list\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res = []   # 分组后的字符串\n",
    "        n = len(s)\n",
    "        curr = 0   # 每个分组的起始下标\n",
    "        # 拆分字符串\n",
    "        while curr < n:\n",
    "            res.append(s[curr:curr+k])\n",
    "            curr += k\n",
    "        # 尝试填充最后一组\n",
    "        res[-1] += fill * (k - len(res[-1]))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res = []\n",
    "        n  =len(s)\n",
    "        curr = 0\n",
    "        while curr<n:\n",
    "            res.append(s[curr:curr+k])\n",
    "            curr+=k\n",
    "        res[-1] += fill*(k-len(res[-1]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        n = len(s)\n",
    "        if n % k:\n",
    "            s += fill * (k - n % k)\n",
    "            n = len(s)\n",
    "        ret = [s[i * k: i * k + k] for i in range(n // k)]\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        if len(s)%k:\n",
    "            for i in range(k - len(s)%k):\n",
    "                s += fill\n",
    "        retList = []\n",
    "        for i in range(0,len(s),k):\n",
    "            retList.append(s[i:i+k])\n",
    "        return retList\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        result=[]\n",
    "        for i in range(0,len(s)//k):\n",
    "            result.append(s[(k*i):(k*i+k)])\n",
    "        if len(s)%k!=0:\n",
    "            str1=s[len(s)//k*k:len(s)]\n",
    "            while(len(str1)<k):\n",
    "                str1=str1+fill\n",
    "            result.append(str1)\n",
    "        return(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ans = []\n",
    "        idx = 0\n",
    "        while idx < len(s):\n",
    "            t = s[idx:idx + k]\n",
    "            idx += k\n",
    "            if len(t) < k:\n",
    "                t += fill * (k - len(t))\n",
    "            ans.append(t)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        res=[]\n",
    "        for i in range(0,len(s),k):\n",
    "            if i+k<=len(s):\n",
    "                res.append(s[i:i+k])\n",
    "            else:\n",
    "                t=s[i:]+\"\".join([fill]*(k-(len(s)-i)))\n",
    "                res.append(t)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        if len(s)%k != 0:\n",
    "            for i in range(k - len(s)%k):\n",
    "                s = s + fill\n",
    "        output = []\n",
    "        for i in range(int(len(s)/k)):\n",
    "            output.append(s[i*k:(i+1)*k])\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divideString(self, s: str, k: int, fill: str) -> List[str]:\n",
    "        ans=[]\n",
    "        for i in range(len(s)//k):\n",
    "            ans.append(s[i*k:(i+1)*k])\n",
    "        if len(s)%k!=0:\n",
    "            ans.append(s[(len(s)//k)*k:])\n",
    "            while len(ans[-1])<k:\n",
    "                ans[-1]=ans[-1]+fill\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
